Summary: This optimization script runs the portfolio with all possible argument combinations and calculates the Average PnL % at the end of each run. The script guarantees that the optimal set of parameter arguments will be found.
' Assign the script parameter to a script variable. _findMaximum = findMaximum ' Get the number of items in the optimization vector. _vectorLength = OptimizationVectorItemCount() ' Count the maximum number of possible optimization vectors. _maxVectors = 1 For i As Integer = 0 To _vectorLength - 1 _maxVectors *= OptimizationVectorItemOptionCount(i) Next ' Define the statistics request that is used to calculate the optimization goal. StatsRequest(-1,DateTimeStart(),DateTimeEnd(),True,True,True,True,useTrades)
' Return the maximum number of optimization vectors. Return _maxVectors
' Calculate the optimization goal. If (_findMaximum) Then Return StatsAverageProfitLossPercent() Else Return -1 * StatsAverageProfitLossPercent() End If
' Use for the result optimization vector. Define result(_vectorLength - 1) As Number ' If this isn 't the first run then read the last optimization vector. If (OptimizationVectorCount() > 0) Then result = OptimizationVector(OptimizationVectorCount() - 1) Else ' If this is the first run then initialize and return the first vector with the lowest values. For itemIndex As Integer = 0 To _vectorLength - 1 result(itemIndex) = OptimizationVectorItemLow(itemIndex) Next Return result End If ' Iterate over all of the vector items while increasing the vector by one step. For itemIndex As Integer = 0 To _vectorLength - 1 ' Use for the highest value of the current vector item. Define itemHigh As Number = OptimizationVectorItemHigh(itemIndex) ' Use for the lowest value of the current vector item. Define itemLow As Number = OptimizationVectorItemLow(itemIndex) ' Use for the step value value of the current vector item. Define itemStep As Number = OptimizationVectorItemStep(itemIndex) ' If the current vector item can be increased by one step then do so. If (result(itemIndex) + itemStep <= itemHigh) Then result(itemIndex) = result(itemIndex) + itemStep Exit For Else ' otherwise reset the current vector item to its lower value and try to increase the next item. result(itemIndex) = itemLow End If Next Return result